<?php
class  Admin_manage extends CI_Model
{
	function __construct()
	{
		parent::__construct();
	}
	
	function edit_category($cate_id)
	{
		$sql="select `classify_id`,`type` from `classify` where `classify_id`='$cate_id'";
		$query=$this->db->query($sql);
		return $query->row_array();	
	}
	function update_category()
	{
		$msg=$this->input->post('sub');
		if(isset($msg))
		{
			$cate_id=$this->input->post('cate_id');
			$cate_name=$this->input->post('cate');
			$sql="update `classify` set `type`='$cate_name' where `classify_id`='$cate_id'";
			if($this->db->query($sql))
			{
				redirect(site_url('manage/right_top'), 'refresh ', 302);
			}	
		}
		else 
		{
		    redirect(site_url('manage/update_category'), 'refresh ', 302);	
		}
	}
	function delete_category($cate_id)
	{
		$sql="delete from `classify`  where `classify_id`='$cate_id'";
		if($this->db->query($sql))
			{
				$sql2="delete from `slave`  where `type_id`='$cate_id'";
				if($this->db->query($sql2))
				{
				redirect(site_url('manage/right_top'), 'refresh ', 302);
				}
			}	
	}
	function insert_scan_admin()
	{
			$sub=$this->input->post('submit');
			if(isset($sub)){
				$usrname=$this->input->post('usrname');
				$pwd=$this->input->post('pwd');
				$pwd=md5(md5($pwd));
				$data = array(
	           		   'uid' =>'0',   
				'admin_name' => $usrname,
	             'admin_pwd' => $pwd,
				 'permit'    => 3
	            );
				if($this->db->insert('admin', $data))
				{
					$data['msg']=array('title'=>'Admin','redirect'=>'welcome/add_scan_admin');
					$this->load->view('add_success',$data);
				}
			}
			else
		    {
		    	redirect(site_url('welcome/add_scan_admin/'), 'refresh ', 302);	
		    }
	}
	function get_category()
	{//Get the category
		$sql="select `classify_id`,`type` from `classify` where `tree`=1 order by classify_id asc";
		$query=$this->db->query($sql);
		if($query->num_rows()>0)
		{
			foreach($query->result() as $cate)
			{
				$cate_id=$cate->classify_id;
				$bc=$cate->type;
				$sql="select * from `classify` where `fid`=$cate_id";
				$query=$this->db->query($sql);
				if($query->num_rows>0)
				{
					foreach ($query->result_array() as $val)
					{
						$se_cate[]=array('cate_id'=>$val['classify_id'],'bc'=>$val['type']);
					}
					$category[]=array('cate_id'=>$cate_id,'bc'=>$bc,'se_cate'=>$se_cate);
					$se_cate='';   
					//unset($se_cate);//上面的方法也可以
				}
				else 
				{
					$category[]=array('cate_id'=>$cate_id,'bc'=>$bc);
				}
			}
			return $category;
		}
		else 
		{
			return false;//no category records
		}
	}
}



































